package com.sky.controller.user;

import com.sky.context.BaseContext;
import com.sky.entity.AddressBook;
import com.sky.result.Result;
import com.sky.service.AddressBookService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/user/addressBook")
@Slf4j
@Api(tags = "地址簿接口")
public class AddressBookController {

    @Autowired
    private AddressBookService addressBookService;

    @GetMapping("/list")
    @ApiOperation("查询用户所有地址")
    public Result<List<AddressBook>> list(){
        Long id = BaseContext.getCurrentId();
        List<AddressBook> list = addressBookService.lambdaQuery()
                .eq(AddressBook::getUserId, id)
                .list();
        return Result.success(list);
    }
    @PostMapping
    @ApiOperation("新增用户地址")
    public Result add(@RequestBody AddressBook addressBook){
        Long id = BaseContext.getCurrentId();
        addressBook.setUserId(id);
        addressBookService.save(addressBook);
        return Result.success();
    }

    @PutMapping
    @ApiOperation("修改用户地址")
    public Result update(@RequestBody AddressBook addressBook){
        addressBookService.updateById(addressBook);
        return Result.success();
    }

    @DeleteMapping
    @ApiOperation("删除用户地址")
    public Result Delete(Long id){
        addressBookService.removeById(id);
        return Result.success();
    }

    @GetMapping("/{id}")
    @ApiOperation("回显用户地址")
    public Result<AddressBook> selectById(@PathVariable Long id){
        AddressBook addressBook = addressBookService.getById(id);
        return Result.success(addressBook);
    }

    @PutMapping("/default")
    @ApiOperation("设置默认地址")
    public Result defaultId(@RequestBody AddressBook addressBook){
        Long id = BaseContext.getCurrentId();

        List<AddressBook> list = addressBookService.lambdaQuery()
                .eq(AddressBook::getUserId, id)
                .list();

        list.stream().map((item) -> {
            item.setIsDefault(0);
            addressBookService.updateById(item);
            return item;
        }).collect(Collectors.toList());

        AddressBook book = addressBookService.lambdaQuery()
                .eq(AddressBook::getId, addressBook.getId())
                .eq(AddressBook::getUserId, id)
                .one();
        book.setIsDefault(1);
        addressBookService.updateById(book);
        return Result.success();
    }

    @GetMapping("/default")
    @ApiOperation("查询默认地址")
    public Result<AddressBook> findDefault(){
        Long id = BaseContext.getCurrentId();
        AddressBook isDefautl = addressBookService.lambdaQuery()
                .eq(AddressBook::getUserId, id)
                .eq(AddressBook::getIsDefault, 1)
                .one();
        AddressBook addressBook = addressBookService.getById(isDefautl.getId());
        return Result.success(addressBook);

    }
}
